package com.amat0urheu.controller.adminController;


import com.alibaba.fastjson.JSONObject;
import com.amat0urheu.pojo.CloudPojo.Flavor;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.FlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class FlavorsController {
    @Autowired
    FlavorService flavorService;
    @PreAuthorize("hasAuthority('ROLE_admin')")
        @GetMapping("/cloud/getAllFlavors/{pageSize}/{pageNum}")
    public ReturnRes getAllFlavors(@PathVariable int pageSize,
                                   @PathVariable int pageNum) {


        return     flavorService.getAllFlavors(pageSize,pageNum);
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/cloud/getAllFlavorsName")
    public ReturnRes getAllFlavorsName() {


        return     flavorService.getAllFlavorsName();
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/admin/cloud/saveFlavor")
    public ReturnRes CreateFlavor() {

        return null;
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/cloud/saveFlavor")
    public ReturnRes RequestParam(@RequestParam(name = "data") String flavorJson) {
        Flavor flavor = JSONObject.parseObject(flavorJson, Flavor.class);
        return  flavorService.CreateFlavor(flavor);
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping("/cloud/refreshFlavor")
    public ReturnRes refreshFlavor() {
        return   flavorService.refreshFlavor();
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/admin/cloud/removeflavor")
    public ReturnRes removeFlavor(@RequestParam(name = "flavorName") String flavorName ) {
        return   flavorService.removeFlavor(flavorName);
    }
}